<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>创建影子 DOM</title>
</head>
<body>
  <div id="foo"></div>
  <div id="bar"></div>
  <script>
    const foo = document.querySelector('#foo');
    const bar = document.querySelector('#bar');

    const openShadowDOM = foo.attachShadow({ mode: 'open' });
    const closedShadowDOM = bar.attachShadow({ mode: 'closed' });  // 创建保密影子

    console.log(openShadowDOM);  // #shadow-root (open)
    console.log(closedShadowDOM);  // #shadow-root (closed)

    console.log(foo.shadowRoot);  // #shadow-root (open)
    console.log(bar.shadowRoot);  // null
  </script>
</body>
</html>